package codeTop;

public class q135 {
    //方法一
    public int candy1(int[] ratings) {
        int n = ratings.length;
        int[] candy = new int[n];
        for (int i = 0; i <n ; i++) {
            boolean flag = true;
            if (i>0&&ratings[i]>ratings[i-1]){
                flag = false;
            }
            if (i<n-1&&ratings[i]>ratings[i+1]){
                flag = false;
            }
            if (flag) {
                candy[i] = 1;
            }
        }
        for (int i = 0; i < n; i++) {
            if (candy[i] == 1) {
                //向左扩展
                int idx = i;
                while (idx > 0 && ratings[idx - 1] > ratings[idx]) {
                    candy[idx-1] = Math.max(candy[idx-1],candy[idx]+1);
                    idx--;
                }
                //向右扩展
                idx = i;
                while (idx < n - 1 && ratings[idx + 1] > ratings[idx]) {
                    candy[idx+1] = Math.max(candy[idx+1],candy[idx]+1);
                    idx++;
                }
            }
        }
        int res = 0;
        for (int i : candy) {
            res+=i;
        }
        return res;
    }

}
